package net.azure.app.view;

import com.jgoodies.binding.adapter.AbstractTableAdapter;
import com.jgoodies.binding.list.ArrayListModel;
import com.jgoodies.binding.list.SelectionInList;
import net.azure.app.model.Story;
import net.azure.app.model.AzureSession;
import net.azure.app.Azure;

import javax.swing.*;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: Venkatraman Kandaswamy : kvenkatraman@gmail.com
 * Date: 12-Jun-2008
 * Time: 07:04:44
 */
public class GridView implements AzureView {

    public void renderView(Component component, Azure azure) {
        ArrayListModel arrayListModel = new ArrayListModel();
        for (Story story : AzureSession.getStories().values()) {
            arrayListModel.add(story);
        }
        SelectionInList selectionInList = new SelectionInList((ListModel) arrayListModel);
        JTable table = new JTable(new DisplayTechnologyTableAdapter(selectionInList, AzureSession.getHeader()));
        table.setShowGrid(true);
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(table.getPreferredSize());
        ((JPanel) component).add(table, BorderLayout.CENTER);
    }

    private class DisplayTechnologyTableAdapter extends AbstractTableAdapter {
        public DisplayTechnologyTableAdapter(ListModel listModel, String[] header) {
            super(listModel, header);
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            Story story = (Story) getRow(rowIndex);
            return story.field(columnIndex);
        }
    }
}
